package com.industrial.operation.service.impl;


import cn.hutool.core.date.DateUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.operation.domain.SysDataReporting;
import com.industrial.operation.mapper.ResultEvaluationRankingMapper;
import com.industrial.operation.service.IResultEvaluationRankingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResultEvaluationRankingServiceImpl implements IResultEvaluationRankingService {

    @Autowired
    private ResultEvaluationRankingMapper resultEvaluationRankingMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询排名列表
     *
     * @param sysDataReporting
     * @return
     */
    @Override
    public List<SysDataReporting> list(SysDataReporting sysDataReporting) {
        //如果不传年份，默认是取的上一年的数据
        if(StringUtils.isBlank(sysDataReporting.getYear())){
            sysDataReporting.setYear(String.valueOf(DateUtil.year(DateUtils.getNowDate()) - 1));
        }
        List<SysDataReporting> list = resultEvaluationRankingMapper.selectResultEvaluationRanking(sysDataReporting);
        for(SysDataReporting item : list){
            String province = item.getProvince();
            String city = item.getCity();
            String district = item.getDistrict();
            StringBuilder addressStr = new StringBuilder();
            if(StringUtil.isNotBlank(province)){
                addressStr.append(stringRedisTemplate.opsForValue().get("area:" + province));
            }
            if(StringUtil.isNotBlank(city)){
                addressStr.append("-").append(stringRedisTemplate.opsForValue().get("area:" + city));
            }
            if(StringUtil.isNotBlank(district)){
                addressStr.append("-").append(stringRedisTemplate.opsForValue().get("area:" + district));
            }
            item.setAddressArea(addressStr.toString());
        }
        return list;
    }
}
